package _16_设计;

import java.util.Iterator;

public class _284_窥探迭代器 {

    // 对于泛性类，直接将Integer改变成泛性
    class PeekingIterator implements Iterator<Integer> {

        private Iterator<Integer> iterator;

        private Integer nextEle;

        public PeekingIterator(Iterator<Integer> iterator) {
            // initialize any member here.
            this.iterator = iterator;
            nextEle = iterator.next();
        }

        // Returns the next element in the iteration without advancing the iterator.
        public Integer peek() {
            return nextEle;
        }

        // hasNext() and next() should behave the same as in the Iterator interface.
        // Override them if needed.
        @Override
        public Integer next() {
            Integer next = nextEle;
            nextEle = iterator.hasNext()? iterator.next(): null;
            return next;
        }

        @Override
        public boolean hasNext() {
            return nextEle != null;
        }
    }
}
